var ctx;
var userAccount = "";

function setCtx(ctx){
	this.ctx = ctx;
}

function gotoPage(page){
	userAccount = ifNull($("#userAccount").val());
	//解决ie9以下placeholder与value重复的问题
	if(userAccount == $("#userAccount").attr("placeholder")){
		userAccount = "";
	}
	loadContent("/couponInfo/couponApply?pNo="+ page +"&userAccount="+ userAccount);
}

function ifNull(mess){
	if(mess == null || mess == undefined){
		mess = "";
	}
	
	return mess;
}

//验证优惠券名称
function checkName(){
	var name = $("#name").val();
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	if(name == null || name == ""){
		layer.tips('名称不能为空!', '#name', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}


//验证开始时间
function checkBeginDate(){
	var beginDate = $("#beginDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(beginDate == $("#beginDate").attr("placeholder")){
		beginDate = "";
	}
	if(beginDate == null || beginDate == ""){
		layer.tips('请选择开始时间!', '#beginDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证结束时间
function checkEndDate(){
	var endDate = $("#endDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(endDate == $("#endDate").attr("placeholder")){
		endDate = "";
	}
	if(endDate == null || endDate == ""){
		layer.tips('请选择结束时间!', '#endDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证开始时间
function checkUseBeginDate(){
	var useBeginDate = $("#useBeginDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(useBeginDate == $("#useBeginDate").attr("placeholder")){
		useBeginDate = "";
	}
	if(useBeginDate == null || useBeginDate == ""){
		layer.tips('请选择开始时间!', '#useBeginDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证结束时间
function checkUseEndDate(){
	var useEndDate = $("#useEndDate").val();
	//解决ie9以下placeholder与value重复的问题
	if(useEndDate == $("#useEndDate").attr("placeholder")){
		useEndDate = "";
	}
	if(useEndDate == null || useEndDate == ""){
		layer.tips('请选择结束时间!', '#useEndDate', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证满减金额
function checkBreakLine(){
	var breakLine = $("#breakLine").val();
	//解决ie9以下placeholder与value重复的问题
	if(breakLine == $("#breakLine").attr("placeholder")){
		breakLine = "";
	}
	var re = /^\d+(\.\d+)?$/ ;
    if (!re.test(breakLine))
   {
    	layer.tips('请填写数字!', '#breakLine', {
		    tips: 3,
		    time:1000
		});
    	$("#breakLine").val("");
    	return false;
    	
    }else if(breakLine.value <= 0){
    	layer.tips('请填写大于0的数字!', '#breakLine', {
		    tips: 3,
		    time:1000
		});
    	$("#breakLine").val("");
    	return false;
    }
	else{
		return true;
	}
}
//验证减免线
function checkBreakPrice(){
	var breakPrice = $("#breakPrice").val();
	var breakLine = $("#breakLine").val();
	//解决ie9以下placeholder与value重复的问题
	if(breakPrice == $("#breakPrice").attr("placeholder")){
		breakPrice = "";
	}
	var re = /^\d+(\.\d+)?$/ ;
    if (!re.test(breakPrice))
   {
    	layer.tips('请填写数字!', '#breakPrice', {
		    tips: 3,
		    time:1000
		});
    	$("#breakPrice").val("");
    	return false;
    	
    }else if(parseInt(breakPrice) > parseInt(breakLine)){
    	layer.tips('请填写不大于'+breakLine+'的数字!', '#breakPrice', {
		    tips: 3,
		    time:1000
		});
    	$("#breakPrice").val("");
    	return false;
    }
	else{
		return true;
	}
}

//验证数量
function checkCount(){
	var count = $("#count").val();
	//解决ie9以下placeholder与value重复的问题
	if(count == $("#count").attr("placeholder")){
		count = "";
	}
	var re = /^\d+(\.\d+)?$/ ;
    if (!re.test(count))
   {
    	layer.tips('请填写数字!', '#count', {
		    tips: 3,
		    time:1000
		});
    	$("#count").val("");
    	return false;
    	
    }else{
		return true;
	}
}

// 保存或新增方法
function saveCouponInfo() {
    var couponInfoId = $("#couponInfoId").val();
    // var cellid = $("#cellid").val();
    // var shopId = $("#shopid").val();
    var name = $("#name").val();
    var breakLine = $("#breakLine").val();
    var breakPrice = $("#breakPrice").val();
    var beginDate = $("#beginDate").val();
    var endDate = $("#endDate").val();
    var useBeginDate = $("#useBeginDate").val();
    var useEndDate = $("#useEndDate").val();
    var count = $("#count").val();

    if (endDate < useEndDate) {
        if (checkName() && checkCount() && checkBeginDate() && checkEndDate()
                && checkUseBeginDate() && checkUseEndDate()
                && checkBreakPrice() && checkBreakLine()) {
            $.ajax({
                type : "POST",
                url : ctx + "/coupon/saveOrUpdateCouponInfo",
                cache : false,
                dataType : 'json',
                data : {
                    id : couponInfoId,
                    name : name,
                    breakLine : breakLine,
                    breakPrice : breakPrice,
                    beginDate : beginDate,
                    endDate : endDate,
                    count : count,
                    useBeginDate : useBeginDate,
                    useEndDate : useEndDate
                },
                success : function(result) {
                    if (result.success) {
                        layer.msg("操作成功", {
                            icon : 1,
                            time : 2000
                        // 2秒关闭（如果不配置，默认是3秒）
                        }, function() {
                            loadContent("/coupon/couponApply");
                        });
                    } else {
                        layer.msg("操作失败", {
                            icon : 2
                        });
                    }
                }
            });
        }
    } else {
        layer.msg("领取结束时间要小于使用结束时间！！！", {
            icon : 2
        });
    }
}

//返回上一页
function cancel(){
	loadContent("/coupon/couponApply"); 
}